package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.ManySheetOneSyler;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.pojo.OutstockDetailedReport;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsOrderChannel;
import com.jy.wms.service.WmsOutstockDetailedReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出库明细controller
 * Created by syh on 2018/4/11.
 */
@Controller
@RequestMapping("/Outstock")
@Api(value = "/Outstock", description = "关于出库明细报表")
public class WmsOutstockDetailedReportController extends BaseController<WmsOutstockDetailedReportService,OutstockDetailedReport> {
    /**
     * 出库明细报表 分页查询信息
     * @param outstockDetailedReport
     * @param session
     * @return
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "出库明细报表 分页查询信息",response = JSONObject.class, notes = "POST出库明细报表分页查询信息")
    public @ResponseBody Map getInstock(@RequestBody OutstockDetailedReport outstockDetailedReport, HttpSession session) {
        Map map2=new HashMap();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
        String hzid=null;
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
        if (hz.size() > 0) {
            hzid=hz.get(0).getHzid();
        }
        try {
            outstockDetailedReport.setHzid(hzid);
            outstockDetailedReport.setCkid(ckid);
            //取值
            Map<String, Object> map1 = baseService.baseSelectPage(outstockDetailedReport);
            map2.put("rows", map1.get("contentList"));
            map2.put("total", map1.get("totals"));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return map2;
    }

    /**
     * 出库报表管理 根据订单渠道名称模糊查询信息
     * @param session
     * @param map
     * @return
     */
    @PostMapping(value = "/getDdmc")
    @ApiOperation(value = "出库报表管理 根据订单渠道名称模糊查询信息",response = JSONObject.class,
            notes = "查询输入</br>{\"mc\":\"订单渠道名称模糊查询\"}")
    public @ResponseBody
    JSONArray getDdmc(HttpSession session, @RequestBody Map<Object,String> map) {
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
        String hzid=null;
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
        if (hz.size() > 0) {
            hzid=hz.get(0).getHzid();
        }
        JSONArray array = new JSONArray();
        try {
            List<WmsOrderChannel> list=new ArrayList<>();
            if(map.get("mc")!=null && !"".equals(map.get("mc"))){
                list = baseService.getDdmc(map.get("mc"),hzid,ckid);
            }else{
                list = baseService.getDdmc(null,hzid,ckid);
            }
            array = JSONArray.fromObject(list);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return array;
    }

    /**
     *出库明细报表 分页查询表头
     * @param session
     * @return
     */
    @PostMapping(value = "/listHeader")
    @ApiOperation(value = "出库明细报表 分页查询表头", response = JSONObject.class, notes = "POST出库明细报表分页查询表头")
    public @ResponseBody
    Map listHeader(HttpSession session) {
        Map map2 = new HashMap();
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
        String hzid = null;
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        try {
            //表头
            List<Map<String, Object>> mapList = baseService.listHeader(hzid, ckid);
            map2.put("column", mapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map2;
    }

    /**
     * 出库明细报表 导出信息
     * @param
     * @param session
     * @param response
     * @throws IOException
     */
    @PostMapping(value = "OutstockExport")
    @ApiOperation(value = "出库明细报表 导出信息", response = JSONObject.class, notes = "出库明细报表 导出信息")
    public @ResponseBody void getOutstockExport(@RequestBody OutstockDetailedReport outstockDetailedReport, HttpSession session, HttpServletResponse response) throws Exception {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        outstockDetailedReport.setCkid(ckid);
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        outstockDetailedReport.setHzid(hzid);
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("出库明细报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<ExcelExportEntity> mapList1 = baseService.listHeader2(hzid, ckid);
        List<Map<String, Object>> mapList = baseService.getOutstockExport(outstockDetailedReport);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, mapList1, mapList);
        workbook.write(response.getOutputStream());
    }

}